package com.householdregister.residence.service.impl;

import com.householdregister.common.exception.ServiceException;
import com.householdregister.common.utils.DateUtils;
import com.householdregister.common.utils.SecurityUtils;
import com.householdregister.residence.domain.HouseholdRegisterHistory;
import com.householdregister.residence.domain.HouseholdResidence;
import com.householdregister.residence.mapper.HouseholdRegisterHistoryMapper;
import com.householdregister.residence.mapper.HouseholdResidenceMapper;
import com.householdregister.residence.service.IHouseholdResidenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 户口管理Service业务层处理
 * 
 * @author zyj
 * @date 2022-02-22
 */
@Service
public class HouseholdResidenceServiceImpl implements IHouseholdResidenceService
{
    @Autowired
    private HouseholdResidenceMapper householdResidenceMapper;

    @Autowired
    private HouseholdRegisterHistoryMapper householdRegisterHistoryMapper;
    /**
     * 查询户口管理
     * 
     * @param id 户口管理主键
     * @return 户口管理
     */
    @Override
    public HouseholdResidence selectHouseholdResidenceById(Long id)
    {
        return householdResidenceMapper.selectHouseholdResidenceById(id);
    }

    /**
     * 查询户口管理列表
     * 
     * @param householdResidence 户口管理
     * @return 户口管理
     */
    @Override
    public List<HouseholdResidence> selectHouseholdResidenceList(HouseholdResidence householdResidence)
    {
        return householdResidenceMapper.selectHouseholdResidenceList(householdResidence);
    }

    /**
     * 新增户口管理
     * 
     * @param householdResidence 户口管理
     * @return 结果
     */
    @Override
    public int insertHouseholdResidence(HouseholdResidence householdResidence)
    {
        householdResidence.setCreateBy(SecurityUtils.getUsername());
        householdResidence.setCreateTime(DateUtils.getNowDate());
        int row = householdResidenceMapper.insertHouseholdResidence(householdResidence);
        householdRegisterHistoryMapper.insertHouseholdResidenceHistory(new HouseholdRegisterHistory("创建户籍信息",householdResidence.getId(),0L,SecurityUtils.getUsername()));
        return row;
    }

    /**
     * 修改户口管理
     * 
     * @param householdResidence 户口管理
     * @return 结果
     */
    @Override
    public int updateHouseholdResidence(HouseholdResidence householdResidence)
    {
        householdResidence.setUpdateBy(SecurityUtils.getUsername());
        householdResidence.setUpdateTime(DateUtils.getNowDate());
        householdRegisterHistoryMapper.insertHouseholdResidenceHistory(new HouseholdRegisterHistory("修改户籍信息",householdResidence.getId(),0L,SecurityUtils.getUsername()));
        return householdResidenceMapper.updateHouseholdResidence(householdResidence);
    }

    /**
     * 批量删除户口管理
     * 
     * @param ids 需要删除的户口管理主键
     * @return 结果
     */
    @Override
    public int deleteHouseholdResidenceByIds(Long[] ids)
    {
        if(householdResidenceMapper.selectCountPeopleByHid(ids[0])>0){
           throw new ServiceException("此户籍下存在人员,请迁出所有人员后再注销！");
        }
        householdRegisterHistoryMapper.insertHouseholdResidenceHistory(new HouseholdRegisterHistory("注销户籍",ids[0],0L,SecurityUtils.getUsername()));
        return householdResidenceMapper.deleteHouseholdResidenceByIds(ids);
    }

    /**
     * 删除户口管理信息
     * 
     * @param id 户口管理主键
     * @return 结果
     */
    @Override
    public int deleteHouseholdResidenceById(Long id)
    {
        return householdResidenceMapper.deleteHouseholdResidenceById(id);
    }

    /**
     * 查询总户籍数
     * @return
     */
    @Override
    public String selectCountResidence(){
        return householdResidenceMapper.selectCountResidence();
    }
}
